const viewList = [
  import("./management/menu/index.vue"),
  import("./management/role/index.vue"),
  import("./management/user/index.vue"),
  import("./test/index.vue"),
  import("./feat/websocket/index.vue"),
];

type Views = Record<string, Component>;
type ViewOption = { label: string; value: string };

/**
 * key为组件中定义的name
 */
const views: Views = {};

const viewOptions: ViewOption[] = [];

const initViews = () => {
  const counter = ref(viewList.length);
  return new Promise<null>((resolve) => {
    viewList.forEach((component) => {
      component.then((comp) => {
        const key = comp.default.name;
        if (!key) throw new Error("组件名称缺失");
        if (views[key]) throw new Error("组件名称重复！" + key);

        views[key] = () => component;
        viewOptions.push({ label: key, value: key });
        counter.value--;
        if (counter.value === 0) {
          resolve(null);
        }
      });
    });
  });
};

export { initViews, views, viewOptions };
